require "lfs"
require "unity"
local MPIModel = require "MPIModel"
local RCSPost = require "RCSPost"
local CreateRadome = require "luaCreateRadome"
local nl = require"luaNumeric"
local QGallop = require"QGMainWindow"

-----------------------------------------------------------------------------
--初始化参数
local RadomeAnalyzer = MPIModel:new( { 
	console_mode = false;
	numHeight = 20;
} );
-----------------------------------------------------------------------------

-----------------------------------------------------------------------------
--[[创建表和创建元表
function RadomeAnalyzer:new (o)
	o = o or {};
	self.__index = self;
	setmetatable(o,self)
	return o;
end
--]]---------------------------------------------------------------------------


-----------------------------------------------------------------------------
--导入模型
function RadomeAnalyzer:load ( path )
	Geometry.clear_all();
	self.path = path;
	self.radome = Geometry.load_step(path);
	-- Geometry.AddShape(self.radome)
	self:perspective_view()
	print(path)
	
	self.analyzer = CreateRadome.Radome2DAnalyzer:new();
	
	assert(self.analyzer:import_from_file(path)>=0, "import file error!\n"..path );
	
	self:perspective_view()

	assert(self.analyzer:isSolid(), "导入模型不包含实体!" );
	assert(self.analyzer:numSolid()==1, "导入模型超过一个实体!" );
	assert(self.analyzer:checkOrientation(), " 天线罩大端未朝向x轴正向" );
	self:perspective_view()
	
	self:init_plot();

	self:calculate_height();
	
	self:calculate_profile();
	

end
-----------------------------------------------------------------------------

-----------------------------------------------------------------------------
--分析厚度
function RadomeAnalyzer:calculate_height ()
	-----------------------------------------------------
	-- calculate the height array
	
	------------------------------------------------------
	--x array
	local xmin = self.analyzer:x_min();
	local xmax = self.analyzer:x_max();
	
	print(xmin,xmax,math.floor(self.numHeights))
	self.xarr = self:LinearSpace(xmin,xmax,self.numHeights)
	self.harr = self:new_vector()

	
	self.x2 = self:new_vector()
	self.y2 = self:new_vector()
	self.x1 = self:new_vector();
	self.y1 = self:new_vector()

	-----------------------------------------------------

	self.analyzer:calculate_height(self.xarr,self.harr,self.x1,self.y1,self.x2,self.y2);
	
	-- print(xarr)
	-- print(harr)
	-- local mw = QGallop.getMainWindow();

	-- local view2 = mw:create2DView("height view:"..self.path);
	self.heightView:clearGraphs()
	gp2 = self.heightView:addGraph(self.xarr,self.harr,"法向壁厚")
	self.heightView:rescaleAxes();
	-----------------------------------------------------
end
-----------------------------------------------------------------------------

-----------------------------------------------------------------------------
--分析厚度
function RadomeAnalyzer:calculate_profile ()
	-----------------------------------------------------
	-- plot the porfile of radome
	local view = self.profileView;
	view:clearGraphs()
	local  xout = nl.new_vector()
	local  yout = nl.new_vector()
	assert( self.analyzer:pnts_outer(xout,yout)>=0, "outer error" );
	gp1 = view:addGraph(xout,yout,"外型面")

	local  xin = nl.new_vector()
	local  yin = nl.new_vector()
	assert( self.analyzer:pnts_inner(xin,yin)>=0, "inner error" );
	gp2 = view:addGraph(xin,yin,"内型面")
	
	view:rescaleAxes();

	view: setAspectRatio();
	
	
	local  ptx = nl.new_vector()
	local  pty = nl.new_vector()
	ptx:resize(2)
	pty:resize(2)
	for ii=1,#self.x1 do
		ptx[1] = self.x1[ii];
		ptx[2] = self.x2[ii];
		pty[1] = self.y1[ii];
		pty[2] = self.y2[ii];
		local gp = view:addGraph(ptx,pty)
		gp:removeFromLegend();	    
		--set_graph({ gp = gp1, name="外形面", color={1,0,0} })	
	end

	view: setAspectRatio();
	-----------------------------------------------------
end

function RadomeAnalyzer:init_plot()
	 self.heightView.xAxis:setProperty( {name="x坐标 [mm]"} )
	 self.heightView.yAxis:setProperty( {name="法向壁厚 [mm]"} )
	 self.heightView:rescaleAxes();
	 
	 self.profileView.xAxis:setProperty( {name="x坐标 [mm]"} )
	 self.profileView.yAxis:setProperty( {name="y坐标 [mm]"} )
	 self.profileView:rescaleAxes();	
	 
	 self.heightView:replot();
	 self.profileView:replot();	
	 
end

function RadomeAnalyzer:clear_curves()
	 self.init_plot()	 
end


return RadomeAnalyzer;
